package hotel.base.oversea.utils.redis;

import com.github.jedis.lock.JedisLock;
import hotel.base.oversea.config.PropertyUtil;
import hotel.base.oversea.constant.CommonBaseConstants;
import lombok.extern.slf4j.Slf4j;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisSentinelPool;

import java.util.HashSet;
import java.util.Set;

/**
*@Description: common 当前项目专用
*@Author: Zhang·jz
*@date: 2020/10/29
*/
@Slf4j
public class RedisSentinelUtil {


    //redis 哨兵相关
    public static String redisMasterName= PropertyUtil.getProperty("common.redis.masterName");
    public static String redisSentinel1= PropertyUtil.getProperty("common.redis.sentinel_1");
    public static String redisSentinel2= PropertyUtil.getProperty("common.redis.sentinel_2");
    public static String redisSentinel3= PropertyUtil.getProperty("common.redis.sentinel_3");
    public static String redisPass= PropertyUtil.getProperty("common.redis.pass");


    // 可用连接实例的最大数目，默认值为8；
    // 如果赋值为-1，则表示不限制；如果pool已经分配了maxActive个jedis实例，则此时pool的状态为exhausted(耗尽)。

    private static int MAX_ACTIVE = 1024;

    // 控制一个pool最多有多少个状态为idle(空闲的)的jedis实例，默认值也是8。
    private static int MAX_IDLE = 200;

    // 等待可用连接的最大时间，单位毫秒，默认值为-1，表示永不超时。如果超过等待时间，则直接抛出JedisConnectionException；
    private static int MAX_WAIT = 10000;
    private static int TIMEOUT = 10000;

    // 在borrow一个jedis实例时，是否提前进行validate操作；如果为true，则得到的jedis实例均是可用的；
    private static boolean TEST_ON_BORROW = true;


    public static String getRedisTokenByAppname(String appname){
        String appKey = appname+"_token";
        String appToken = "";
        Set<String> sentinelSet =new HashSet<String>();
        sentinelSet.add(redisSentinel1);
        sentinelSet.add(redisSentinel2);
        sentinelSet.add(redisSentinel3);

        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxTotal(MAX_ACTIVE);
        config.setMaxWaitMillis(MAX_WAIT);
        config.setMaxIdle(MAX_IDLE);
//        config.setTestOnBorrow(TEST_ON_BORROW);
//        config.setTestOnReturn(true);

        JedisSentinelPool sentinelPool = new JedisSentinelPool(redisMasterName,sentinelSet,config);
        Jedis jedis = null;
        try {
            jedis = sentinelPool.getResource();
            jedis.auth(redisPass);
            jedis.select(CommonBaseConstants.REDIS_DB_5);
            if(jedis.exists(appKey)){
                appToken = jedis.get(appKey).toString();
            }
            log.info("appname{}===appToken{}",appname,appToken);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            log.info("<<<<<<<<commom connetion jedis is close!>>>>>>>>");
            jedis.close();
        }
        sentinelPool.close();

        log.info("<<<<<<<<commom connetion sentinelPool is close!>>>>>>>>");
        return appToken;
    }

    public static void setRedisTokenByAppname(String appname,String token,int expiresIn){
        String appKey = appname+"_token";
        log.info("appname{}===appToken{}",appname,token);
        Set<String> sentinelSet =new HashSet<String>();
        sentinelSet.add(redisSentinel1);
        sentinelSet.add(redisSentinel2);
        sentinelSet.add(redisSentinel3);

        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxTotal(MAX_ACTIVE);
        config.setMaxWaitMillis(MAX_WAIT);
        config.setMaxIdle(MAX_IDLE);
//        config.setTestOnBorrow(TEST_ON_BORROW);

        JedisSentinelPool sentinelPool = new JedisSentinelPool(redisMasterName,sentinelSet,config);
        Jedis jedis = null;
        JedisLock jedisLock= null;
        try {
            jedis = sentinelPool.getResource();
            jedis.auth(redisPass);
            jedis.select(CommonBaseConstants.REDIS_DB_5);

            jedisLock=new JedisLock(jedis,appKey,2000,2000);
            String retStr = jedis.set(appKey,token,"NX", "EX",expiresIn);
            log.info("retStr>>{}",retStr);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(jedisLock!=null){
                //释放锁
                jedisLock.release();
            }
            jedis.close();
            log.info("<<<<<<<<commom connetion jedis is close!>>>>>>>>");
        }
        sentinelPool.close();
        log.info("<<<<<<<<commom connetion sentinelPool is close!>>>>>>>>");
    }





    public static String getRedisTokenByPro(String appname){
        String appKey = appname+"_token";
        String appToken = "";
        Set<String> sentinelSet =new HashSet<String>();
        sentinelSet.add("114.55.243.97:26379");
        sentinelSet.add("121.196.11.76:26379");
        sentinelSet.add("47.97.62.21:26379");
        JedisPoolConfig config = new JedisPoolConfig();
        JedisSentinelPool sentinelPool = new JedisSentinelPool(redisMasterName,sentinelSet,config);
        Jedis jedis = null;
        try {
            jedis = sentinelPool.getResource();
            jedis.auth("henhaoban888");
            jedis.select(CommonBaseConstants.REDIS_DB_5);
            if(jedis.exists(appKey)){
                appToken = jedis.get(appKey).toString();
            }
            log.info("appname{}===appToken{}",appname,appToken);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            jedis.close();
            log.info("<<<<<<<<commom connetion jedis is close!>>>>>>>>");
        }
        sentinelPool.close();
        log.info("<<<<<<<<commom connetion sentinelPool is close!>>>>>>>>");

        return appToken;
    }
    public static  void main(String[] arg){
//        setRedisTokenByAppname("switch","2222222222222",100);
        getRedisTokenByPro("quartz");
    }
}
